<template>
  <div class="w-full flex items-center">
    <el-date-picker
      v-model="beginTime"
      :type="dateType"
      :placeholder="props.placeholder"
      clearable
      class="mb-[0px]"
      v-if="dateType != 'date'"
      :value-format="valueFormat"
    />
    <el-date-picker
      v-model="beginTime"
      type="date"
      :placeholder="props.placeholder"
      clearable
      :value-format="valueFormat"
      class="mb-[0px]"
      v-else
    />
    <span style="width: 20px; text-align: center">至</span>
    <el-date-picker
      v-model="endTime"
      :type="dateType"
      :placeholder="props.placeholder"
      clearable
      :value-format="valueFormat"
      class="mb-[0px]"
      v-if="dateType != 'date'"
    />
    <el-date-picker
      v-model="endTime"
      type="date"
      :placeholder="props.placeholder"
      clearable
      :value-format="valueFormat"
      class="mb-[0px]"
      v-else
    />
    <el-select v-model="dateType" style="flex-basis: 145px; flex-shrink: 0" @change="typeChange">
      <el-option :label="item.label" :value="item.type" v-for="item in selectOptions" :key="item.type" />
    </el-select>
  </div>
</template>

<script setup>
const beginTime = defineModel('beginTime')
const endTime = defineModel('endTime')
const dateFormat = defineModel('dateformat')
const dateType = ref('date')
const valueFormat = ref('YYYY-MM-DD')
const props = defineProps({
  placeholder: {
    type: String,
    default: '',
  },
})
const selectOptions = [
  {
    label: '年月日',
    type: 'date',
    dateFormat: 'yyyy-MM-dd',
    valueFormat:'YYYY-MM-DD'
  },
  {
    label: '年月',
    type: 'month',
    dateFormat: 'yyyy-MM',
    valueFormat:'YYYY-MM'
  },
  {
    label: '年度',
    type: 'year',
    dateFormat: 'yyyy',
    valueFormat:'YYYY'
  },
]
//选择的时间类型改变
const typeChange = (value) => {
  beginTime.value = ''
  endTime.value = ''
  dateFormat.value = selectOptions.find((a) => a.type === value).dateFormat
  valueFormat.value = selectOptions.find((a) => a.type === value).valueFormat
}
</script>

<style scoped lang="scss"></style>
